home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Mac Game Programming Gurus / TricksOfTheMacGameProgrammingGurus.iso / Book Chapters / 03 - Advanced Graphics / Example 2 / sprite.c < prev    next >
Text File  |  1995-06-02  |  5KB  |  202 lines

  1. //
  2. //    File: sprite.c
  3. //
  4. //    This file contains the routines to draw the sprites
  5. //
  6. //    2/19/95 -- Created by Mick
  7. //
  8.  
  9. // include files
  10.  
  11. #include "global.h"
  12.  
  13. #include "sprite.h"
  14.  
  15. #include "main.h"
  16.  
  17. // defines for this file
  18.  
  19. // global function declarations
  20.  
  21. tSpriteInfo *loadSprite( signed short inSpriteResID, signed short inMaskResID );
  22. void disposeSprite( tSpriteInfo *inSpriteInfo );
  23. void startSpriteDraw( Rect *inClipRect, PixMapHandle inDestPixMap );
  24. void endSpriteDraw( void );
  25. void drawSprite( tSpriteInfo *inSpriteInfo, Point inWhere );
  26.  
  27. // global data owned by this file
  28.  
  29. // local function declarations
  30.  
  31. // static data
  32.  
  33. RgnHandle sOldClipRgn;                                // storage space for the clip region before the startSpriteDraw call
  34. PixMapHandle sDestPixMap;                // where we are going to draw this data
  35.  
  36. // functions
  37.  
  38.  
  39. //
  40. //    loadSprite -
  41. //
  42. //    Loads/allocates all the data for a sprite.
  43. //
  44.  
  45. tSpriteInfo *loadSprite( signed short inSpriteResID, signed short inMaskResID )
  46. {
  47.     tSpriteInfo *newSprite;            // the new sprite data
  48.     PicHandle spritePict;                        // the sprite/mask picture
  49.     CGrafPtr oldCPort;                    // the graf port that is in place when we are called
  50.     GrafPtr oldPort;                // the b&w port that we are saving
  51.     GDHandle oldDevice;            // the gdevice that is in place when we are called
  52.     
  53.     // create the sprite info record
  54.     newSprite = ( tSpriteInfo * )NewPtr( sizeof( tSpriteInfo ) );
  55.     
  56.     // load the pict
  57.     spritePict = GetPicture( inSpriteResID );
  58.     if ( spritePict == ( PicHandle )kNil )
  59.         {
  60.             // if it did not load, drop into the debugger -- real programs would have error checking
  61.             Debugger();
  62.         }
  63.     
  64.     // copy its bounds rect
  65.     newSprite->fSpriteRect = ( *spritePict )->picFrame;
  66.     
  67.     // create a gworld for it
  68.     NewGWorld( &newSprite->fSpriteWorld, 8, &( newSprite->fSpriteRect ), 
  69.             gAppColorTable, ( GDHandle )kNil, keepLocal );
  70.     
  71.     // extract the pixmap handle
  72.     newSprite->fSpritePix = GetGWorldPixMap( newSprite->fSpriteWorld );
  73.     
  74.     // save the current port and gdevice
  75.     GetGWorld( &oldCPort, &oldDevice );
  76.     
  77.     // set the offscreen buffer as current ( and lock the pixel map )
  78.     SetGWorld( newSprite->fSpriteWorld, ( GDHandle )kNil );
  79.     LockPixels( newSprite->fSpritePix );
  80.     
  81.     // draw the picture
  82.     EraseRect( &( newSprite->fSpriteRect ) );
  83.     DrawPicture( spritePict, &( newSprite->fSpriteRect ) );
  84.     
  85.     // restore the current port and gdevice
  86.     UnlockPixels( newSprite->fSpritePix );
  87.     SetGWorld( oldCPort, oldDevice );
  88.  
  89.     // dump this pict
  90.     ReleaseResource( ( Handle )spritePict );
  91.     
  92.     // load the mask pict
  93.     spritePict = GetPicture( inMaskResID );
  94.  
  95.     // allocate a new port
  96.     newSprite->fMaskPort = ( GrafPtr )NewPtr( sizeof( GrafPort ) );
  97.     OpenPort( newSprite->fMaskPort );
  98.     
  99.     // fiddle with its port bits
  100.     newSprite->fMaskPort->portBits.bounds = newSprite->fSpriteRect;
  101.     newSprite->fMaskPort->portBits.rowBytes = ( ( newSprite->fSpriteRect.right - newSprite->fSpriteRect.left ) + 15 ) / 8;
  102.     newSprite->fMaskPort->portBits.baseAddr = 
  103.             NewPtr( newSprite->fMaskPort->portBits.rowBytes * ( newSprite->fSpriteRect.bottom - newSprite->fSpriteRect.top ) );
  104.  
  105.     // save the port
  106.     GetPort( &oldPort );
  107.     
  108.     // set the new port
  109.     SetPort( newSprite->fMaskPort );
  110.     
  111.     // draw the picture
  112.     EraseRect( &( newSprite->fSpriteRect ) );
  113.     DrawPicture( spritePict, &( newSprite->fSpriteRect ) );
  114.     
  115.     // restore the old port
  116.     SetPort( oldPort );
  117.  
  118.     // dump this pict
  119.     ReleaseResource( ( Handle )spritePict );
  120.     
  121.     // return the new sprite!
  122.     return newSprite;
  123. }
  124.  
  125.  
  126. //
  127. //    disposeSprite -
  128. //
  129. //    Disposes/releases all the memory used in a sprite
  130. //
  131.  
  132. void disposeSprite( tSpriteInfo *inSpriteInfo )
  133. {
  134.     // dump the gworld
  135.     DisposeGWorld( inSpriteInfo->fSpriteWorld );
  136.     
  137.     // dump the memory from the bitmap
  138.     DisposePtr( ( Ptr )( inSpriteInfo->fMaskPort->portBits.baseAddr ) );
  139.     
  140.     // dump the port
  141.     DisposePtr( ( Ptr )( inSpriteInfo->fMaskPort ) );
  142.     
  143.     // free the structure
  144.     DisposePtr( ( Ptr )inSpriteInfo );
  145. }
  146.  
  147.  
  148. //
  149. //    startSpriteDraw -
  150. //
  151. //    Prepare the sprite draw. Assumes that the port is set to the destination port.
  152. //
  153.  
  154. void startSpriteDraw( Rect *inClipRect, PixMapHandle inDestPixMap )
  155. {
  156.     // save the current port's clip region
  157.     sOldClipRgn = NewRgn();
  158.     GetClip( sOldClipRgn );
  159.     
  160.     // set the clip region to be the passed in rect
  161.     ClipRect( inClipRect );
  162.     
  163.     // save the pix map info (so we can use it)
  164.     sDestPixMap = inDestPixMap;
  165. }
  166.  
  167.  
  168. //
  169. //    endSpriteDraw -
  170. //
  171. //    End the sprite draw sequence.
  172. //
  173.  
  174. void endSpriteDraw( void )
  175. {
  176.     // restore the old clip region
  177.     SetClip( sOldClipRgn );
  178.     
  179.     // dump the region
  180.     DisposeRgn( sOldClipRgn );
  181. }
  182.  
  183.  
  184. //
  185. //    drawSprite -
  186. //
  187. //    Draw the sprite in the port.
  188. //
  189.  
  190. void drawSprite( tSpriteInfo *inSpriteInfo, Point inWhere )
  191. {
  192.     Rect destRect;        // where we want to draw the sprite
  193.     
  194.     // calculate the destination rect
  195.     destRect = inSpriteInfo->fSpriteRect;
  196.     OffsetRect( &destRect, inWhere.h, inWhere.v );
  197.     
  198.     // draw the sprite
  199.     CopyMask( ( BitMap * )( *( inSpriteInfo->fSpritePix ) ), &( inSpriteInfo->fMaskPort->portBits ), 
  200.             ( BitMap * )( *sDestPixMap ), &( inSpriteInfo->fSpriteRect ), &( inSpriteInfo->fSpriteRect ), &destRect );
  201. }
  202.